﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using TestMySqlConnetPoll;
using View.Common;
using System.Threading;

namespace View.Journal.UserView
{
    /// <summary>
    /// 用户浏览记录控件
    /// </summary>
    public partial class UserViewForm : DevExpress.XtraEditors.XtraForm
    {
        //用户浏览记录Business
        private UserScanTable uScanTable = new UserScanTable();
        private DataTable sourceTable = new DataTable();
        private DataTable sourceTable_temp = new DataTable();
        private UserViewLogTYPE viewType;

        public UserViewForm(string strTitle, UserViewLogTYPE type)
        {
            InitializeComponent();
            this.Text = strTitle;
            viewType = type;
            InitTable();
            SetTimeList();
            
            this.grid_main.DataSource = viewList;
        }

        private void SetTimeList()
        {
            this.comboBox_time.Items.Clear();
            DateTime dt = DateTime.Now;

            for (int i = 1; i <= 60; i++)
            {
                this.comboBox_time.Items.Add(UtilityFunc.DateTimeStringFormat(dt, TimeFormatType.DAY));
                dt = dt.AddDays(-1);
            }

            this.comboBox_time.SelectedIndex = 0;
        }

        private void InitTable()
        {
            sourceTable.Columns.Add("name");
            sourceTable.Columns.Add("openTime");
            sourceTable.Columns.Add("closeTime");
            sourceTable.Columns.Add("totalTime");
            sourceTable.Columns.Add("uName");

           

            foreach (string strName in c_Device.UserScanAnnalConfig[Enum.GetName(typeof(UserViewLogTYPE), viewType)])
            {
                UserView view = new UserView();
                view.name = strName;
                viewList.Add(view);
                sourceTable.Rows.Add(new object[] { strName, "", "", "" });
            }

            sourceTable_temp = sourceTable.Copy();
        }

        /// <summary>
        /// 未浏览记录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CheckBox_noview_CheckedChanged(object sender, EventArgs e)
        {
            viewList_noview.Clear();
            //viewList_noview = UtilityFunc.Clone(viewList) as BindingList<UserView>;
            if (checkBox_noview.Checked)
            {
                foreach (UserView view in viewList)
                {
                    if (view.IsNoView())
                        viewList_noview.Add(UtilityFunc.Clone(view) as UserView);

                }

                this.grid_main.DataSource = viewList_noview;
            }
            else
            {
                this.grid_main.DataSource = viewList;
            }
        }

        /// <summary>
        /// 刷新
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnReflash_Click(object sender, EventArgs e)
        {
            GetDataSource();
        }

        /// <summary>
        /// 查询对应数据
        /// </summary>
        private void GetDataSource()
        {
            ClearViewList();

            if (viewType == UserViewLogTYPE.设备浏览记录)
                uScanTable.Read_ValueList("设备浏览记录", DateTime.Parse((string)this.comboBox_time.SelectedItem));
            else if (viewType == UserViewLogTYPE.报警浏览记录)
                uScanTable.Read_ValueList("报警浏览记录", DateTime.Parse((string)this.comboBox_time.SelectedItem));
            else
                uScanTable.Read_ValueList("道岔动作电流曲线浏览记录", DateTime.Parse((string)this.comboBox_time.SelectedItem));


            while (!uScanTable.Ready_ValueList)
            {
                if (uScanTable.ErrorType != 0)
                    return;

                Thread.Sleep(20);
            }

            foreach (string strKey in uScanTable.ValueList.Keys)
            {
                UserView view = viewList.SingleOrDefault(v => v.name == strKey);
                if (view == null)
                    continue;
                int rowIndex = viewList.IndexOf(view);
                int index = 0;
                foreach (objTableScanAnnal tab in uScanTable.ValueList[strKey])
                {
                    DateTime dts = MySql.Time_tToDateTime(tab.s_time);
                    DateTime dte = MySql.Time_tToDateTime(tab.e_time);
                    TimeSpan span =  dte.Subtract(dts);
                    if (index == 0)
                    {
                        view.openTime = UtilityFunc.DateTimeStringFormat(dts, TimeFormatType.DCGONGLV);
                        view.closeTime = UtilityFunc.DateTimeStringFormat(dte, TimeFormatType.DCGONGLV);
                        view.totalTime = string.Format("{0:00}:{1:00}:{2:00}", span.Hours, span.Minutes, span.Seconds);
                        view.uName = tab.UserName;
                    }
                    else
                    {
                        UserView viewTemp = new UserView();
                        viewTemp.name = view.name;
                        viewTemp.openTime = UtilityFunc.DateTimeStringFormat(dts, TimeFormatType.DCGONGLV);
                        viewTemp.closeTime = UtilityFunc.DateTimeStringFormat(dte, TimeFormatType.DCGONGLV);
                        viewTemp.totalTime = string.Format("{0:00}:{1:00}:{2:00}", span.Hours, span.Minutes, span.Seconds);
                        viewTemp.uName = tab.UserName;
                        rowIndex++;
                        viewList.Insert(rowIndex, viewTemp);
                    }
                    index++;
                }
            }

            this.grid_main.DataSource = viewList;
            this.grid_main.RefreshDataSource();
        }

        private void ClearViewList()
        {
            foreach(UserView uView in viewList)
            {
                uView.ClearView();
            }

            viewList = viewList.Distinct(new ProductIdComparer()).ToList();
        }

        /// <summary>
        /// 查询时间改变
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ComboBox_time_SelectedIndexChanged(object sender, EventArgs e)
        {
            GetDataSource();
        }

        /// <summary>
        /// 添加行号
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void GridView_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e)
        {
            if (e.Info.IsRowIndicator && e.RowHandle >= 0)
            {
                e.Info.DisplayText = (e.RowHandle + 1).ToString().Trim();
            }
        }

        List<UserView> viewList = new List<UserView>();
        List<UserView> viewList_noview = new List<UserView>();

        public class ProductIdComparer : IEqualityComparer<UserView>
        {
            public bool Equals(UserView x, UserView y)
            {
                if (x == null)
                    return y == null;
                return x.name == y.name;
            }

            public int GetHashCode(UserView obj)
            {
                if (obj == null)
                    return 0;
                return obj.name.GetHashCode();
            }
        }

        /// <summary>
        /// 浏览记录数据对象
        /// </summary>
        [Serializable]
        public class UserView
        {
            public UserView()
            {
                name =string.Empty;
                openTime =string.Empty;
                closeTime =string.Empty;
                totalTime =string.Empty;
                uName = string.Empty;

            }

            public void ClearView()
            {
                openTime = string.Empty;
                closeTime = string.Empty;
                totalTime = string.Empty;
                uName = string.Empty;
            }

            public string uName { get; set; }
            public string name { get; set; }
            public string openTime { get; set; }
            public string closeTime { get; set; }
            public string totalTime { get; set; }

            public bool IsNoView()
            {
                if (totalTime == string.Empty)
                    return true;
                else
                    return false;
            }
        }


    }

  
}